package com.cxxx.guns.modular.backend.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.cxxx.guns.core.base.controller.BaseController;
import com.cxxx.guns.core.support.BeanKit;
import com.cxxx.guns.core.util.ToolUtil;
import com.cxxx.guns.modular.backend.common.Const;
import com.cxxx.guns.modular.backend.model.Quickimg;
import com.cxxx.guns.modular.backend.service.IQuickimgService;
import com.cxxx.guns.modular.backend.util.getSubStrUtils;
import com.cxxx.guns.modular.backend.warpper.NewsWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.cxxx.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.cxxx.guns.modular.backend.model.News;
import com.cxxx.guns.modular.backend.service.INewsService;

import java.util.Date;
import java.util.List;

/**
 * 新闻管理控制器
 *
 * @author fengshuonan
 * @Date 2018-09-04 14:08:22
 */
@Controller
@RequestMapping("/news")
public class NewsController extends BaseController {

    private String PREFIX = "/backend/news/";

    @Autowired
    private INewsService newsService;
    @Autowired
    private IQuickimgService quickimgService;

    /**
     * 跳转到新闻管理首页
     */
    @RequestMapping("")
    public String index(Model model) {
        List list = Const.getModelList();
        model.addAttribute("list",list);
        return PREFIX + "news.html";
    }

    /**
     * 跳转到添加新闻管理
     */
    @RequestMapping("/news_add")
    public String newsAdd(Model model) {
        List list = Const.getModelList();
        model.addAttribute("list",list);
        return PREFIX + "news_add.html";
    }

    /**
     * 跳转到修改新闻管理
     */
    @RequestMapping("/news_update/{newsId}")
    public String newsUpdate(@PathVariable Integer newsId, Model model) {
        News news = newsService.selectById(newsId);
        model.addAttribute("news",news);
        List list = Const.getModelList();
        model.addAttribute("list",list);
        LogObjectHolder.me().set(news);
        return PREFIX + "news_edit.html";
    }

    /**
     * 跳转到修改新闻管理
     */
    @RequestMapping("/news_detail/{newsId}")
    public String newsDetail(@PathVariable Integer newsId, Model model) {
        News news = newsService.selectById(newsId);
        model.addAttribute("news",news);
        LogObjectHolder.me().set(news);
        return PREFIX + "news_detail.html";
    }

    /**
     * 获取新闻管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(value = "condition", required = false) String condition,
                       @RequestParam(value = "modelId",required = false ) Integer modelId) {

        List list =  null;
        list = newsService.getList(condition, modelId);
        return new NewsWarpper(BeanKit.listToMapList(list)).warp();
    }

    /**
     * 新增新闻管理
     * modify by yyi
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(News news) {
        Date date = new Date();
        news.setCreateTime(date);
        newsService.addNews(news);
        String content = news.getNewsContent();
        /**取出第一张图片的信息**/
        List firstImgs = getSubStrUtils.getImgStr(content);
        if (firstImgs.size()!=0){
           String firstImg = (String)firstImgs.get(0);
            Quickimg quickimg = new Quickimg();
            quickimg.setDate(date);
            quickimg.setModel(news.getModel());
            quickimg.setNewsId(news.getId());
            //得到的urlhttp://140.143.166.110/upload/417760f9-a4f9-48e4-9e02-a01be7bfea01.png
           // String split[] = firstImg.split("upload/");
            quickimg.setUrl(firstImg);
            quickimgService.insert(quickimg);
        }
        return SUCCESS_TIP;
    }

    /**
     * 删除新闻管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer newsId) {
        /**
         * 根据newsId查出图片的id，然后删除
         * **/
        News news = newsService.selectById(newsId);
        if(news!=null){
            Wrapper wrapper = new EntityWrapper();
            wrapper.eq("newsId",news.getId());
            quickimgService.delete(wrapper);
        }

        newsService.deleteById(newsId);
        return SUCCESS_TIP;
    }

    /**
     * 修改新闻管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(News news) {
        newsService.updateById(news);
        return SUCCESS_TIP;
    }

    /**
     * 新闻管理详情
     */
    @RequestMapping(value = "/detail/{newsId}")
    @ResponseBody
    public Object detail(@PathVariable("newsId") Integer newsId) {
        return newsService.selectById(newsId);
    }
}
